package com.example.bill.sys;

import android.util.Log;
import android.util.Printer;

import java.util.HashMap;

public class LooperPrinter implements Printer {

    public static final String FINISHED_TO = "<<<<< Finished to ";
    public static final String DISPATCHING_TO = ">>>>> Dispatching to ";
    private final HashMap<String, Long> action2TimeStamp = new HashMap<>();

    public LooperPrinter() {



    }

    @Override
    public void println(String x) {


        if (x.startsWith(DISPATCHING_TO)) {
            String[] split = x.substring(DISPATCHING_TO.length()).split(": ");
            action2TimeStamp.put(split[0], System.currentTimeMillis());


        } else if (x.startsWith(FINISHED_TO)) {
            String actionName = x.substring(FINISHED_TO.length());
            Long lastTimeStamp = action2TimeStamp.remove(actionName);
            if (lastTimeStamp!=null) {
                long actionDuration = System.currentTimeMillis() - lastTimeStamp;
                if(actionDuration>16){

                    Log.w("MyLooperPrinter",actionName+" 执行耗时:"+ actionDuration +"毫秒");
                }
            }

        }
    }
}
